﻿function addToDo() {
    var string = $("#string")
    if (string.val() == "") {
        alert("请输入：");
        return;
    }
    var user = { "string": string.val(), "state": false };
    var data = getData();
    data.push(user);
    saveData(data);
    showData();
    string.val("");
}

function clearData() {
    localStorage.clear();
    showData();
}

function getData() {
    var data = localStorage.getItem("todoList");
    if (data != null) {
        return JSON.parse(data);
    }
    else
        return [];
}
function saveData(data) {
    localStorage.setItem("todoList", JSON.stringify(data));

}

function deleteToDo(index) {
    var data = getData();
    data.splice(index, 1);
    saveData(data);
    showData();
}

function showData() {
    var table = $("#todoList");
    var table1 = $("#doneList");
    var data = localStorage.getItem("todoList");
    if (data != null) {
        var todoList = JSON.parse(data);
        var todoCount = 0;
        var doneCount = 0;
        var str = "";
        var str1 = "";
        for (var i = 0; i < todoList.length; i++) {
            if (todoList[i].state == false) {
                str += "<input type=\"checkbox\" style=\"cursor:pointer\" onclick=\"transgerToDo(" + i + ")\" />" +
                    "<tr><td>" + todoList[i].string + "</td></tr>" +
                    "<span class=\"t7\" style=\"cursor:pointer\" onclick=\"deleteToDo(" + i + ")\">" +
                    "<img src=\"../img/1.jpg\" alt=\"删除图片\" width=\"15px\" heigth=\"15px\"/></span></br>";
                todoCount++;
            }
            else {
                str1 += "<input type=\"checkbox\" style=\"cursor:pointer\" onclick=\"transgerToDo(" + i + ")\" />" +
                    "<tr><td>" + todoList[i].string + "</td></tr>" +
                    "<span class=\"t7\" style=\"cursor:pointer\" onclick=\"deleteToDo(" + i + ")\">" +
                    "<img src=\"../img/1.jpg\" alt=\"删除图片\" width=\"15px\" heigth=\"15px\"/> </span></br>";
                doneCount++;
            }
        };
        todocount.innerHTML = todoCount;
        donecount.innerHTML = doneCount;

        table.html(str);
        table1.html(str1);
    }
    else {
        todocount.innerHTML = 0;
        donecount.innerHTML = 0;
        table.html("");
        table1.html("");
    }
}

function transgerToDo(index) {
    var data = getData();
    data[index].state = !data[index].state;
    saveData(data);
    showData();
}

$(document).ready(function () {
    showData();
})